package com.project.web.order.controller.app.webNative;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.project.common.core.utils.exception.Result;
import com.project.web.order.controller.common.BaseController;
import com.project.web.order.model.app.MbrOrder;
import com.project.web.order.model.app.vo.MbrOrderPayVO;
import com.project.web.order.service.app.MbrOrderService;
import com.project.web.util.exception.ORDER_RESP_CODE_ENUM;
import com.project.web.util.exception.OrderCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 会员订单API
 *
 * @Author: wyy
 * @Date: 2021-02-02 20:19
 */
@Api(tags = "【Web】课程订单API")
@RestController("mbrOrderController")
@RequestMapping("/order/auth/web")
public class MbrOrderController extends BaseController {

    /**
     * 日志工具类
     */
    Logger logger = LoggerFactory.getLogger(MbrOrderController.class);

    @Resource(name = "mbrOrderServiceImpl")
    private MbrOrderService mbrOrderService;

    /**
     * 分页查询课程订单
     *
     * @param mbrOrder 课程订单对象
     * @return
     */
    @ApiOperation(value = "分页查询课程订单", notes = "分页查询课程订单", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "课程订单", value = "mbrOrder", dataType = "MbrOrder", required = false),
            @ApiImplicitParam(name = "页数", value = "pageNum", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "每页条数", value = "pageSize", dataType = "Integer", required = true),
    })
    @GetMapping(value = "/getPageList")
    public Result getPageList(MbrOrder mbrOrder, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        PageInfo<MbrOrder> pageList = mbrOrderService.getPageList(mbrOrder, page);
        return getResult(pageList);
    }

    /**
     * 小程序商品预下单
     *
     * @param mbrOrderPayVO 会员APP订单
     * @return
     */
    @ApiOperation(value = "小程序商品预下单")
    @PostMapping(value = "/unifiedOrderOfMiniProduct")
    public Result<Map<String, Object>> unifiedOrderOfMiniProduct(@RequestBody MbrOrderPayVO mbrOrderPayVO, HttpServletRequest httpRequest) {
        // 参数校验
        String userId = mbrOrderPayVO.getUserId();
        BigDecimal actualAmt = mbrOrderPayVO.getActualAmt();
        BigDecimal totalAmt = mbrOrderPayVO.getTotalAmt();
        Integer payMode = mbrOrderPayVO.getPayMode();
        if (mbrOrderPayVO == null || StringUtils.isBlank(userId) || actualAmt == null || totalAmt == null || payMode == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        log.info("\r\n ************** 小程序商品统一下单参数  \r\n");
        log.info("\r\n {}  \r\n", new Gson().toJson(mbrOrderPayVO));
        // 保存订单
        Map<String, Object> returnMap = mbrOrderService.handleMbrOrderOfMiniProduct(mbrOrderPayVO, httpRequest);
        return getResult(returnMap);
    }
}
